#!/usr/bin/env bash
set -e

echo "设定远程仓库地址..."
if [ ! -d "/root/.ssh" ];then
    if [[ ${ID_RSA} ]];then
    echo "设置私钥，以更新私有代码"
    mkdir -p /root/.ssh  && echo -e ${ID_RSA} > /root/.ssh/id_rsa  && chmod 600 /root/.ssh/id_rsa  && ssh-keyscan gitee.com > /root/.ssh/known_hosts
    fi
fi
cd /scripts
git clone --no-checkout -b ${REPO_BRANCH} ${REPO_URL} /unicomTemp
mv -f /unicomTemp/.git /scripts
rm -rf /unicomTemp
git reset --hard HEAD
echo "git pull拉取最新代码..."
cd  /scripts && git fetch --all && git reset --hard origin/${REPO_BRANCH}
echo "npm install 安装最新依赖"
npm config set registry http://mirrors.cloud.tencent.com/npm/
npm install -s --prefix /scripts
echo "------------------------设置账号信息写入配置文件----------------------------------"
echo "{" > /scripts/default.json
echo "\"user\":\"${user}\"," >> /scripts//default.json
echo "\"password\":\"${password}\"," >> /scripts//default.json
echo "\"appid\": \"${appid}\"" >> /scripts//default.json
echo "}" >> /scripts/default.json

echo "------------------------------------------------执行定时任务任务shell脚本------------------------------------------------"
source /update_crontab.sh
/usr/sbin/crond -c /etc/crontabs -f
